<?php
function pluginsLesen ($out) {
	global $config;
	include $config;
	$pluginOrdner = newsOrdner."plugins";
	if(is_dir($pluginOrdner)) {	
		$po = opendir($pluginOrdner);
		$i = 0;
		$plugins = "";
		$pluginStatus = " ";
		$pluginList = "";
		$pluginEinstellungen = " ";
		$screen = "";
		$status = "";
		while (($file = readdir($po)) !== false) {
			if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue;
			if (!is_dir($pluginOrdner."/".$file)) continue;
			if (!is_file($pluginOrdner."/".$file."/index.php")) continue;
			// Einstellungen lesen
			$puo = opendir($pluginOrdner."/".$file);
			while (($pluginDatei = readdir($puo)) !== false) {
				if ($pluginDatei == "index.php") {
					include ($pluginOrdner."/".$file."/".$pluginDatei);
					break;
				}
			}
			closedir($puo);
			// Einstellungen lesen ende
			if ($out) {
				if (isset($status)) {
					if ($status)  { 
						$pluginStatus = "checked='checked'";
						$status = "true";
					} else {
						$pluginStatus = " ";	
						$status = "false";
					}
				}
				
				if (file_exists(newsOrdner."plugins/".$file."/admin.php")) $pluginEinstellungen = "<a href='?admin=plugins&amp;einstellen=$file'>".CONFIG."</a> | ";
				else $pluginEinstellungen = " ";
				if ($screen == "") {
					$screen = $pluginOrdner."/".$file."/screen.jpg";
					if (file_exists($screen)) $screen = "<img src='$screen' alt='$file'/>";
					else $screen = ADMIN_BACKEND_NOPICTURE;
				}
				$plugins[$i] = array('screenshot' => $screen, 'name' => $name, 'version' => $version, 'autor' => $autor, 'pluginStatus' => $pluginStatus, 'status' => $status, 'id' => $i, 'description' => $beschreibung, 'page' => $website, 'dir' => $file, 'configLink' => $pluginEinstellungen);
				$screen = "";
			} else {
				if ($status) $status = "on";
				else $status = "off";
				$pluginList[$file] = $status;
			}
			++$i;
		}
		closedir($po);
		if (!$out && count($pluginList) > 0) return $pluginList;
		if (count($pluginList) < 1) return false; 
	} 
	if ($out) {
		$token = md5(uniqid(rand(), true));
		$_SESSION['token'] = $token;
		$tmp = new Template(templateUrl.'admin_plugins.tpl');
		$tmp->setContent("PLUGINS",$plugins);
		$tmp->setContent("token",$token);
		echo $tmp->vorlage;
	}
}



function pluginsSpeichern() {
	if (isset($_POST['pluginSatusAend'])) {
		if( empty($_POST['token']) || $_POST['token'] != $_SESSION['token'] ) die;
		$benutzer       = $_SESSION['name'];
		$passwort       = $_SESSION['pass'];
		$konfigurierbar = "";
		if (!loginPruefen($benutzer,$passwort)) {
			echo ADMIN_NOPERMISSION;
			session_destroy();
		} else { 
			$i = 0;
			if (isset($_POST['ordner'])) $ordner = $_POST['ordner'];
			foreach ($ordner as $line) {
				if (isset($_POST['sstatus'][$i])) $aktStatus = "true";
				else $aktStatus = "false";
				$aktuellerOrdner = newsOrdner."plugins/".$line;
				if (is_dir($aktuellerOrdner)) {
					$pluginOrdner = opendir($aktuellerOrdner);
					if (file_exists($aktuellerOrdner."/index.php")) {
						include ($aktuellerOrdner."/index.php");
						if ($konfigurierbar) $skonfigurierbar = "true";
						else $skonfigurierbar = "false";
						$buffer = "// Plugin Info\n// ****************\n\$name = '$name';\n\$autor = '$autor';\n\$version = '$version';\n\$beschreibung = '$beschreibung';\n\$website = '$website';\n\$status = $aktStatus;\n// This file is generated, please make NO changes!!\n";
						// DEBUG ONLY!!
						// $buffer = preg_replace('!\015\012|\015|\012!','<br />',$buffer);
						// echo "<br />",$buffer,"<br />";				
						$buffer = "<?php\n".$buffer."?>";
						$fd = @fopen($aktuellerOrdner."/index.php","w");						
						if ($fd) {
							fwrite ($fd,$buffer); 
							fclose ($fd);
						}	
					}
				}
				++$i;
			}
		}
		unset($_SESSION['token']);
	}
}


function pluginEinstellen($einstellen) {	
	include newsOrdner."plugins/".$einstellen."/admin.php";
}


function pluginIncludes() {
	$plugins = pluginsLesen(false);
	if ($plugins == "") return false;
	while (list($plugin, $status) = each($plugins)) {
		if ($status=="on") {
			$include = newsOrdner."plugins/".$plugin."/includes.php";
			if (file_exists($include)) include $include;
			else continue;
		} else continue;
	}
}

function pluginEintrag(& $titel,& $eintag,& $datum,& $autor) {
	$plugins = pluginsLesen(false);
	if ($plugins == "") return false;
	while (list($plugin, $status) = each($plugins)) {
		if ($status=="on") {
			$action = newsOrdner."plugins/".$plugin."/action.php";
			if (file_exists($action)) include $action;
			else continue;
		} else continue;
	}
}

function pluginKom(& $kommentar,& $kommail,& $komseite,& $eintrag) {
	$plugins = pluginsLesen(false);
	if ($plugins == "") return false;
	while (list($plugin, $status) = each($plugins)) {
		if ($status=="on") {
			$action = newsOrdner."plugins/".$plugin."/action.php";
			if (file_exists($action)) include $action;
			else continue;
		} else continue;
	}
}

?>